
 function [C,A] = C_Savings_PHI(Wi,Nstates,T,CAPH,Pspot_N,Ppaid_N,gamma,rho,r,amax)

    % Ygrid: what are all possible net income in each period
    % each state is really each premium I may possibly carry 
    % this is stacked by time
    premiumgrid = reshape(Pspot_N(1:end-1,:),Nstates*T,1)';
    % now need to construct transition matrices
    %the probal
    
    % first year transition matrix
    % this is the probablility in period t of switching to any spot premium
    % in t+1
    
    
    megaCAP = zeros(Nstates*T,Nstates*T,T-1);
    for t = 1:T-1
    
        % three conditions: spot needs to correspond to next period
        %low = 25*t+1;
        %high = low+24;
        low = Nstates*t+1;
        high = low+(Nstates-1);
              
        isnextp = zeros(Nstates*T,Nstates*T);
        isnextp(:,low:high) = 1;        
        % spot has to be lower
        lower = repmat(premiumgrid',1,Nstates*T)>repmat(premiumgrid,Nstates*T,1);

        % has to transit
        megaCAPt = repmat(CAPH(1:end-1,1:end-1,t),T,T).*isnextp.*lower;

        % guaranteed renewability    %if none of the conditions are met I can keep the premium i have

        megaCAPt = megaCAPt+diag(1-sum(megaCAPt,2));    

        megaCAP(:,:,t) = megaCAPt;

    end
    
    Ygrid = repmat(Wi,1,Nstates*T)-repmat(premiumgrid,T,1);

    %multiply all this by CAPH(t)
    
    Ygrid = permute(Ygrid, [3 2 1]); 

    Abar   = 0;                          % Borrowing constraint (min level of assets)
    agrid3 = linspace(Abar,amax*1000,101);

    Ygrid = Ygrid*1000;
        
    [AT2, CT2]=PoliFunctions(Ygrid,agrid3,T,gamma,rho,r,megaCAP);

    %iX is the premium paid that is simulated
    
    [~,iX]=ismember(Ppaid_N,premiumgrid);    
    
    n = Nstates*T;
    
    % if not, meaning it is a 0, which means death
    
    iX(iX==0) = Nstates*T+1;
    
    %a = unique(iX);
    %out = [a,histc(iX(:),a)];
    
    [Rows, Cols] = size(iX);

    sims = Cols;
    
    
        yhistt2 = zeros(T,sims);
        ahistt2 = zeros(T,sims);
        dhistt2 = zeros(T,sims);
        chistt2 = zeros(T,sims);
        
        iXt = iX(1,:);
        iXt(iXt==n+1)=1;
        ahistt2(1,:) = AT2(iXt,1);
        yhistt2(1,:) = Ygrid(1,iXt,1);
        chistt2(1,:) = CT2(iXt,1);
        dhistt2(1,:) = zeros(1,sims);
     
        for i = 2:T
        
            % a_ind is 1xsims that gives the column number 
            % for the asset in the previous period
     
        a= repmat(ahistt2(i-1,:)',1,101)==repmat(agrid3,sims,1);
        a_ind = sum(a.*repmat(1:101,sims,1),2)';
    
            %iXt gives the row number
        iXt = iX(i,:);
        dhistt2(i,:) = iXt==(n+1);
        % replace the index for 1 if index is     
        iXt(iXt==n+1)=1;
        yhistt2(i,:) = Ygrid(1,iXt,i).*(1-dhistt2(i,:));
        AT2t = AT2(:,:,i);
        CT2t = CT2(:,:,i);
        % linear index
        index = n*(a_ind-1)+iXt;
        ahistt2(i,:) = AT2t(index).*(1-dhistt2(i,:));
        chistt2(i,:) = CT2t(index).*(1-dhistt2(i,:));
        
        end
    
    %YF2 = [YF2;yhistt2];
    %CF2 = [CF2;chistt2];
    %AF2 = [AF2;ahistt2];
    %DF2 = [DF2;dhistt2];
    
    %end
    
    C = chistt2;
    A = ahistt2;
   
    
 end
 
 